<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>axios常用配置以及默认配置</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.1.2/axios.js"></script>
  </head>
  <body>
    <button>王者荣耀</button>
    <button>英雄联盟</button>
    <button>添加书籍</button>
    <script>
        //获取按钮
        let btns = document.querySelectorAll('button');
        //对 axios 进行默认值的设置
        axios.defaults.baseURL = 'http://api.xiaohigh.com';
        axios.defaults.headers = {
            token: 'xxxxxxxxx'
        }

        //第一个按钮
        btns[0].onclick = async function(){
            let response = await axios.get('/heros');
            console.log(response);
        }

        //第二个按钮
        btns[1].onclick = async function(){
            let response = await axios.get('/lol');

            console.log(response);
        }

        //第三个按钮
        btns[2].onclick = async function(){
            // let response = await axios.post('http://127.0.0.1:3000/book', 'title=钢铁是怎样炼成的&price=19.9');
            let response = await axios.post('http://127.0.0.1:3000/book', {title: '安徒生童话', price: 8.8});

            console.log(response);
        }


    </script>
  </body>
</html>
